<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>pandas | zylai</title><meta name="keywords" content="python数据分析"><meta name="author" content="zylai"><meta name="copyright" content="zylai"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="Pandas 1、基本介绍 pandas类似于python的字典，一般和numpy搭配着使用。  创建DataFrame，指定行和列的名称 默认名称就是索引号 传入字典构造DataFrame 查看数据类型 查看行和列的名称 转置 排序，按照索引名称排序。按照值排序  123456789101112131415161718192021222324252627282930313233343536373">
<meta property="og:type" content="article">
<meta property="og:title" content="pandas">
<meta property="og:url" content="https://zhaoyunlai.gitee.io/posts/8dfe3ff98cbb/index.html">
<meta property="og:site_name" content="zylai">
<meta property="og:description" content="Pandas 1、基本介绍 pandas类似于python的字典，一般和numpy搭配着使用。  创建DataFrame，指定行和列的名称 默认名称就是索引号 传入字典构造DataFrame 查看数据类型 查看行和列的名称 转置 排序，按照索引名称排序。按照值排序  123456789101112131415161718192021222324252627282930313233343536373">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fwww.geocalculate.com%2Fwp-content%2Fuploads%2F2020%2F07%2Fpandas.png&refer=http%3A%2F%2Fwww.geocalculate.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1653547694&t=2523b5ca63fc839243af3a93c7c2ba71">
<meta property="article:published_time" content="2022-03-08T13:59:07.000Z">
<meta property="article:modified_time" content="2022-08-27T08:24:10.204Z">
<meta property="article:author" content="zylai">
<meta property="article:tag" content="python数据分析">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fwww.geocalculate.com%2Fwp-content%2Fuploads%2F2020%2F07%2Fpandas.png&refer=http%3A%2F%2Fwww.geocalculate.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1653547694&t=2523b5ca63fc839243af3a93c7c2ba71"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="https://zhaoyunlai.gitee.io/posts/8dfe3ff98cbb/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//hm.baidu.com"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><script>var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?ec7714e43e379e663d7258bb72abd0cc";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":false,"languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'pandas',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-08-27 16:24:10'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><!-- hexo injector head_end start --><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Zfour/hexo-electric-clock@1.0.6/clock.css"><!-- hexo injector head_end end --><meta name="generator" content="Hexo 5.4.2"></head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/img/avatar.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">18</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">14</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">11</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fwww.geocalculate.com%2Fwp-content%2Fuploads%2F2020%2F07%2Fpandas.png&amp;refer=http%3A%2F%2Fwww.geocalculate.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=auto?sec=1653547694&amp;t=2523b5ca63fc839243af3a93c7c2ba71')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">zylai</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">pandas</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-03-08T13:59:07.000Z" title="发表于 2022-03-08 21:59:07">2022-03-08</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-08-27T08:24:10.204Z" title="更新于 2022-08-27 16:24:10">2022-08-27</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/python/">python</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="pandas"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1>Pandas</h1>
<h2 id="1、基本介绍">1、基本介绍</h2>
<p>pandas类似于python的字典，一般和numpy搭配着使用。</p>
<ol>
<li>创建DataFrame，指定行和列的名称</li>
<li>默认名称就是索引号</li>
<li>传入字典构造DataFrame</li>
<li>查看数据类型</li>
<li>查看行和列的名称</li>
<li>转置</li>
<li>排序，按照索引名称排序。按照值排序</li>
</ol>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"><span class="comment"># pd类似于字典</span></span><br><span class="line"><span class="comment"># 一般为pd和np搭配着使用</span></span><br><span class="line">s = pd.Series([<span class="number">1</span>,<span class="number">3</span>,<span class="number">6</span>,np.nan,<span class="number">44</span>,<span class="number">1</span>])</span><br><span class="line"><span class="comment"># 都是带索引的</span></span><br><span class="line"><span class="built_in">print</span>(s)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 创建一个大的矩阵，在pd中叫DataFrame</span></span><br><span class="line"><span class="comment"># 这里创建一个DataFrame，行的索引为dates，列的索引为abcd，这样，每个数据都有一个名字</span></span><br><span class="line">dates = pd.date_range(<span class="string">&#x27;20220101&#x27;</span>,periods=<span class="number">6</span>)</span><br><span class="line"><span class="built_in">print</span>(dates)</span><br><span class="line">df = pd.DataFrame(np.random.randn(<span class="number">6</span>,<span class="number">4</span>),index=dates,columns=[<span class="string">&#x27;a&#x27;</span>,<span class="string">&#x27;b&#x27;</span>,<span class="string">&#x27;c&#x27;</span>,<span class="string">&#x27;d&#x27;</span>])</span><br><span class="line"><span class="built_in">print</span>(df)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 默认名称就是对应的索引0,1,2等</span></span><br><span class="line">df1 = pd.DataFrame(np.arange(<span class="number">12</span>).reshape((<span class="number">3</span>,<span class="number">4</span>)))</span><br><span class="line"><span class="built_in">print</span>(df1)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 传入字典构造DataFrame</span></span><br><span class="line"><span class="comment"># A对应的只有一条数据，但是为了对齐，补成了4个</span></span><br><span class="line"><span class="comment"># 若都是四条数据（比如BCD），可以允许存在一条的数据（比如A），但不允许存在3条等其他数据</span></span><br><span class="line">df2 = pd.DataFrame(&#123;</span><br><span class="line">    <span class="string">&#x27;A&#x27;</span>:<span class="number">1.</span>,</span><br><span class="line">    <span class="string">&#x27;B&#x27;</span>:pd.Series(<span class="number">1</span>,index=<span class="built_in">list</span>(<span class="built_in">range</span>(<span class="number">4</span>)),dtype=<span class="string">&#x27;float32&#x27;</span>),</span><br><span class="line">    <span class="string">&#x27;C&#x27;</span>:np.array([<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>]),</span><br><span class="line">    <span class="string">&#x27;D&#x27;</span>:np.array([<span class="string">&#x27;test&#x27;</span>,<span class="string">&#x27;train&#x27;</span>,<span class="string">&#x27;test&#x27;</span>,<span class="string">&#x27;train&#x27;</span>]),</span><br><span class="line">    <span class="string">&#x27;E&#x27;</span>:pd.Timestamp(<span class="string">&#x27;20210101&#x27;</span>)</span><br><span class="line">&#125;)</span><br><span class="line"><span class="built_in">print</span>(df2)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;*****************&quot;</span>)</span><br><span class="line"><span class="comment"># 查看数据类型</span></span><br><span class="line"><span class="built_in">print</span>(df2.dtypes)</span><br><span class="line"><span class="comment"># 打印行索引和列索引，index和columns都是属性</span></span><br><span class="line"><span class="built_in">print</span>(df2.index)</span><br><span class="line"><span class="built_in">print</span>(df.columns)</span><br><span class="line"><span class="built_in">print</span>(df2.values)</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;\n********1**********&quot;</span>)</span><br><span class="line"><span class="comment"># describe方法，计算数字型数据的一些属性比如方差平均值等</span></span><br><span class="line"><span class="built_in">print</span>(df2.describe())</span><br><span class="line"></span><br><span class="line"><span class="comment"># 转置</span></span><br><span class="line"><span class="built_in">print</span>(df2.T)</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;\n**********2*********&quot;</span>)</span><br><span class="line"><span class="comment"># 排序</span></span><br><span class="line"><span class="comment"># 1.按照索引名称排序，指定排序的维度和方式</span></span><br><span class="line"><span class="comment"># 这里指定列排序，倒序</span></span><br><span class="line"><span class="built_in">print</span>(df2.sort_index(axis=<span class="number">1</span>,ascending=<span class="literal">False</span>))</span><br><span class="line"><span class="comment"># 这里按照行</span></span><br><span class="line"><span class="built_in">print</span>(df2.sort_index(axis=<span class="number">0</span>,ascending=<span class="literal">False</span>))</span><br><span class="line"></span><br><span class="line"><span class="comment"># 2.按照值排序</span></span><br><span class="line"><span class="built_in">print</span>(df2.sort_values(by=<span class="string">&#x27;D&#x27;</span>))</span><br></pre></td></tr></table></figure>
<h2 id="2、选择数据">2、选择数据</h2>
<ol>
<li>选择列，两种选择方式，[label] 和 .label</li>
<li>loc，通过标签选择</li>
<li>iloc，通过位置选择</li>
<li>Boolean indexing</li>
</ol>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"></span><br><span class="line">dates = pd.date_range(<span class="string">&#x27;20220101&#x27;</span>,periods=<span class="number">6</span>)</span><br><span class="line">df = pd.DataFrame(np.arange(<span class="number">24</span>).reshape((<span class="number">6</span>,<span class="number">4</span>)),index=dates,columns = [<span class="string">&#x27;A&#x27;</span>,<span class="string">&#x27;B&#x27;</span>,<span class="string">&#x27;C&#x27;</span>,<span class="string">&#x27;D&#x27;</span>])</span><br><span class="line"><span class="built_in">print</span>(df)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 选择一列，两种选择方式，[label] 和 .label</span></span><br><span class="line"><span class="built_in">print</span>(df[<span class="string">&#x27;A&#x27;</span>])</span><br><span class="line"><span class="built_in">print</span>(df.A)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;\n********--1--*********&quot;</span>)</span><br><span class="line"><span class="comment"># 切片选择行,只能选择行，不可加个逗号选择列</span></span><br><span class="line"><span class="built_in">print</span>(df[<span class="number">0</span>:<span class="number">3</span>])</span><br><span class="line"><span class="comment"># 注意，这种选择方式可以包括区间右端的值</span></span><br><span class="line"><span class="built_in">print</span>(df[<span class="string">&#x27;20220101&#x27;</span>:<span class="string">&#x27;20220104&#x27;</span>])</span><br><span class="line"></span><br><span class="line"><span class="comment"># -----几种选的方式--------</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;\n********--2--*********&quot;</span>)</span><br><span class="line"><span class="comment"># 通过标签选择 loc</span></span><br><span class="line"><span class="built_in">print</span>(df.loc[<span class="string">&#x27;20220101&#x27;</span>])</span><br><span class="line"><span class="comment"># 选定指定行和列,值的切片包括右侧区间值</span></span><br><span class="line"><span class="built_in">print</span>(df.loc[<span class="string">&#x27;20220102&#x27;</span>:<span class="string">&#x27;20220105&#x27;</span>,[<span class="string">&#x27;A&#x27;</span>,<span class="string">&#x27;B&#x27;</span>]])</span><br><span class="line"></span><br><span class="line"><span class="comment"># 通过位置选择 iloc ，就是和numpy类似了，可以切片</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;\n********--3--*********&quot;</span>)</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(df.iloc[<span class="number">3</span>])<span class="comment">#选择第三行</span></span><br><span class="line"><span class="built_in">print</span>(df.iloc[<span class="number">3</span>:<span class="number">5</span>,<span class="number">1</span>:<span class="number">3</span>])<span class="comment">#切片选择</span></span><br><span class="line"><span class="built_in">print</span>(df.iloc[[<span class="number">1</span>,<span class="number">3</span>,<span class="number">5</span>],<span class="number">1</span>:<span class="number">3</span>])<span class="comment">#选择指定行，切片选择列</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 混合筛选 mixed selection ix，在pandas0.20.0及其以后版本中，ix已经不被推荐使用</span></span><br><span class="line"><span class="comment"># &#x27;DataFrame&#x27; object has no attribute &#x27;ix&#x27; </span></span><br><span class="line"><span class="comment"># print(df.ix[:3,[&#x27;A&#x27;,&#x27;C&#x27;]])</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Boolean indexing</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;\n********--4--*********&quot;</span>)</span><br><span class="line"><span class="built_in">print</span>(df)</span><br><span class="line"><span class="comment"># df.A&lt;8等价于df[&#x27;A&#x27;]&lt;8</span></span><br><span class="line"><span class="built_in">print</span>(df[df.A&lt;<span class="number">8</span>]) </span><br></pre></td></tr></table></figure>
<h2 id="3、设置值">3、设置值</h2>
<ol>
<li>iloc通过位置设置值</li>
<li>loc通过标签设置值</li>
<li>更改符合条件的值</li>
<li>添加一列</li>
</ol>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"></span><br><span class="line">dates = pd.date_range(<span class="string">&#x27;20220101&#x27;</span>,periods=<span class="number">6</span>)</span><br><span class="line">df = pd.DataFrame(np.arange(<span class="number">24</span>).reshape((<span class="number">6</span>,<span class="number">4</span>)),index=dates,columns = [<span class="string">&#x27;A&#x27;</span>,<span class="string">&#x27;B&#x27;</span>,<span class="string">&#x27;C&#x27;</span>,<span class="string">&#x27;D&#x27;</span>])</span><br><span class="line"><span class="built_in">print</span>(df)</span><br><span class="line"><span class="comment"># 1.iloc设置值，通过位置</span></span><br><span class="line">df.iloc[<span class="number">1</span>,<span class="number">1</span>]=<span class="number">111</span></span><br><span class="line"><span class="comment"># 2.loc设置值，通过标签</span></span><br><span class="line">df.loc[<span class="string">&#x27;20220101&#x27;</span>,<span class="string">&#x27;C&#x27;</span>]=<span class="number">222</span></span><br><span class="line"><span class="built_in">print</span>(df)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;\n********--1--*********&quot;</span>)</span><br><span class="line"><span class="comment"># 3.更改符合条件的值</span></span><br><span class="line"><span class="comment"># 更改A列中大于4的值为0</span></span><br><span class="line">df.A[df.A&gt;<span class="number">4</span>]=<span class="number">0</span></span><br><span class="line"><span class="comment"># 等价于df.iloc[:,0][df.iloc[:,0]&gt;4]=0</span></span><br><span class="line"><span class="built_in">print</span>(df)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;\n********--2--*********&quot;</span>)</span><br><span class="line"><span class="comment"># 1.添加一列</span></span><br><span class="line">df[<span class="string">&#x27;F&#x27;</span>]=np.nan</span><br><span class="line"><span class="comment"># 2.添加一个有具体值的一列，这里添加pd的一个序列，并且索引值名称要相对应</span></span><br><span class="line">df[<span class="string">&#x27;E&#x27;</span>]=pd.Series([<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>],index=pd.date_range(<span class="string">&#x27;20220101&#x27;</span>,periods=<span class="number">6</span>))</span><br><span class="line"><span class="built_in">print</span>(df)</span><br></pre></td></tr></table></figure>
<h2 id="4、处理丢失数据">4、处理丢失数据</h2>
<ol>
<li>dropna</li>
<li>fillna</li>
<li>isnull</li>
</ol>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"></span><br><span class="line">dates = pd.date_range(<span class="string">&#x27;20220101&#x27;</span>,periods=<span class="number">6</span>)</span><br><span class="line">df = pd.DataFrame(np.arange(<span class="number">24</span>).reshape((<span class="number">6</span>,<span class="number">4</span>)),index=dates,columns = [<span class="string">&#x27;A&#x27;</span>,<span class="string">&#x27;B&#x27;</span>,<span class="string">&#x27;C&#x27;</span>,<span class="string">&#x27;D&#x27;</span>])</span><br><span class="line">df.iloc[<span class="number">0</span>,<span class="number">1</span>]=np.nan</span><br><span class="line">df.iloc[<span class="number">1</span>,<span class="number">2</span>]=np.nan</span><br><span class="line"><span class="built_in">print</span>(df)</span><br><span class="line"><span class="comment"># 1.使用dropna axis选择除去的维度，how any表示只要这一行有nan我就把这一行丢掉，</span></span><br><span class="line"><span class="comment"># all表示只有这一行都是nan才把这一行丢掉，默认为how=any</span></span><br><span class="line"><span class="comment"># 这里axis=0表示丢掉行</span></span><br><span class="line"><span class="built_in">print</span>(df.dropna(axis=<span class="number">0</span>,how=<span class="string">&#x27;any&#x27;</span>))</span><br><span class="line"></span><br><span class="line"><span class="comment"># 2.fillna是把nan值填充为指定值</span></span><br><span class="line"><span class="built_in">print</span>(df.fillna(value=<span class="number">0</span>))</span><br><span class="line"></span><br><span class="line"><span class="comment"># 3.isnull判断数据是否为nan，返回一个矩阵，对应的元素为nan时该位置的值为True否则为False</span></span><br><span class="line"><span class="built_in">print</span>(df.isnull())</span><br><span class="line"></span><br><span class="line"><span class="comment"># 4.查看是否包含指定值，这里查看是否包含True</span></span><br><span class="line"><span class="built_in">print</span>(np.<span class="built_in">any</span>(df.isnull()==<span class="literal">True</span>))</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="5、导入和导出">5、导入和导出</h2>
<ol>
<li>使用read_文件类型 读入文件</li>
<li>使用to_文件类型 导出文件</li>
<li>具体的参数使用可以查阅官方api</li>
</ol>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line"></span><br><span class="line"><span class="comment"># 如果是vscode编译器，注意终端打开的是终端工作目录的文件夹，直接使用相对路径可能会出错</span></span><br><span class="line"><span class="comment"># 所以使用终端工作路径下的相对路径</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 读取csv文件，默认sep=&#x27;,&#x27;，这里csv使用excel工具生成的，间隔为\t,所以使用sep=&#x27;\t&#x27;去掉</span></span><br><span class="line">data = pd.read_csv(<span class="string">&#x27;./3-pandas/student.csv&#x27;</span>,sep=<span class="string">&#x27;\t&#x27;</span>)</span><br><span class="line"><span class="built_in">print</span>(data)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 导出文件</span></span><br><span class="line">data.to_pickle(<span class="string">&#x27;./3-pandas/student.pickle&#x27;</span>)</span><br></pre></td></tr></table></figure>
<h2 id="6、合并concat">6、合并concat</h2>
<ol>
<li>使用concat合并</li>
<li>axis指定合并的维度</li>
<li><strong>join指定合并的方式</strong></li>
<li>append在末尾增加新行</li>
</ol>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"></span><br><span class="line"><span class="comment"># concatenating</span></span><br><span class="line">df1 = pd.DataFrame(np.ones((<span class="number">3</span>,<span class="number">4</span>))*<span class="number">0</span>,columns=[<span class="string">&#x27;a&#x27;</span>,<span class="string">&#x27;b&#x27;</span>,<span class="string">&#x27;c&#x27;</span>,<span class="string">&#x27;d&#x27;</span>])</span><br><span class="line">df2 = pd.DataFrame(np.ones((<span class="number">3</span>,<span class="number">4</span>))*<span class="number">1</span>,columns=[<span class="string">&#x27;a&#x27;</span>,<span class="string">&#x27;b&#x27;</span>,<span class="string">&#x27;c&#x27;</span>,<span class="string">&#x27;d&#x27;</span>])</span><br><span class="line">df3 = pd.DataFrame(np.ones((<span class="number">3</span>,<span class="number">4</span>))*<span class="number">2</span>,columns=[<span class="string">&#x27;a&#x27;</span>,<span class="string">&#x27;b&#x27;</span>,<span class="string">&#x27;c&#x27;</span>,<span class="string">&#x27;d&#x27;</span>])</span><br><span class="line"><span class="built_in">print</span>(df1)</span><br><span class="line"><span class="built_in">print</span>(df2)</span><br><span class="line"><span class="built_in">print</span>(df3)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;*********--1--***********&quot;</span>)</span><br><span class="line"><span class="comment"># 竖向合并，合并为多行</span></span><br><span class="line"><span class="comment"># axis指定合并的维度，ignore_index是忽略掉原有的索引，将索引合并</span></span><br><span class="line">res = pd.concat([df1,df2,df3],axis=<span class="number">0</span>,ignore_index=<span class="literal">True</span>)</span><br><span class="line"><span class="built_in">print</span>(res)</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;*********--2--***********&quot;</span>)</span><br><span class="line"><span class="comment"># 使用join功能</span></span><br><span class="line">df1 = pd.DataFrame(np.ones((<span class="number">3</span>,<span class="number">4</span>))*<span class="number">0</span>,columns=[<span class="string">&#x27;a&#x27;</span>,<span class="string">&#x27;b&#x27;</span>,<span class="string">&#x27;c&#x27;</span>,<span class="string">&#x27;d&#x27;</span>],index=[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>])</span><br><span class="line">df1 = pd.DataFrame(np.ones((<span class="number">3</span>,<span class="number">4</span>))*<span class="number">0</span>,columns=[<span class="string">&#x27;b&#x27;</span>,<span class="string">&#x27;c&#x27;</span>,<span class="string">&#x27;d&#x27;</span>,<span class="string">&#x27;e&#x27;</span>],index=[<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>])</span><br><span class="line"><span class="comment"># print(df1)</span></span><br><span class="line"><span class="comment"># print(df2)</span></span><br><span class="line"><span class="comment"># 如果直接合并，那么二者不同的部分就会自动填充为nan，等价于join默认为outer</span></span><br><span class="line">res = pd.concat([df1,df2],join=<span class="string">&#x27;outer&#x27;</span>)</span><br><span class="line"><span class="built_in">print</span>(res)</span><br><span class="line"><span class="comment"># 将join设置为inner，就会把不同的部分裁减掉，保留相同部分</span></span><br><span class="line">res = pd.concat([df1,df2],join=<span class="string">&#x27;inner&#x27;</span>,ignore_index=<span class="literal">True</span>)</span><br><span class="line"><span class="built_in">print</span>(res)</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;*********--3--***********&quot;</span>)</span><br><span class="line"><span class="comment"># 左右合并，这里join_axes是按照df1的索引进行合并，df2没有得部分用nan代替</span></span><br><span class="line"><span class="comment"># 但是从 pandas=1.0.0 开始，就不支持 join_axes  参数了，所以这里不再演示，会在merge中学习</span></span><br><span class="line"><span class="comment"># res = pd.concat([df1,df2],axis=1,join_axes=[df1.index])</span></span><br><span class="line"><span class="comment"># print(res)</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># append，在行的末尾加上新行</span></span><br><span class="line">df1 = pd.DataFrame(np.ones((<span class="number">3</span>,<span class="number">4</span>))*<span class="number">0</span>,columns=[<span class="string">&#x27;a&#x27;</span>,<span class="string">&#x27;b&#x27;</span>,<span class="string">&#x27;c&#x27;</span>,<span class="string">&#x27;d&#x27;</span>])</span><br><span class="line">df2 = pd.DataFrame(np.ones((<span class="number">3</span>,<span class="number">4</span>))*<span class="number">1</span>,columns=[<span class="string">&#x27;a&#x27;</span>,<span class="string">&#x27;b&#x27;</span>,<span class="string">&#x27;c&#x27;</span>,<span class="string">&#x27;d&#x27;</span>])</span><br><span class="line">df3 = pd.DataFrame(np.ones((<span class="number">3</span>,<span class="number">4</span>))*<span class="number">2</span>,columns=[<span class="string">&#x27;a&#x27;</span>,<span class="string">&#x27;b&#x27;</span>,<span class="string">&#x27;c&#x27;</span>,<span class="string">&#x27;d&#x27;</span>])</span><br><span class="line">res =  df1.append([df2,df3],ignore_index=<span class="literal">True</span>)</span><br><span class="line"><span class="built_in">print</span>(res)</span><br><span class="line"><span class="comment"># 添加一行，series</span></span><br><span class="line">s1 = pd.Series([<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>],index=[<span class="string">&#x27;a&#x27;</span>,<span class="string">&#x27;b&#x27;</span>,<span class="string">&#x27;c&#x27;</span>,<span class="string">&#x27;d&#x27;</span>])</span><br><span class="line">res = df1.append(s1,ignore_index=<span class="literal">True</span>)</span><br><span class="line"><span class="built_in">print</span>(res)</span><br></pre></td></tr></table></figure>
<h2 id="7、合并merge">7、合并merge</h2>
<ol>
<li>on指定依据哪一列进行合并</li>
<li><code>how</code>指定合并的方式，left，right，outer，inner</li>
<li>indicator显示数据时如何合并的</li>
<li>根据index合并</li>
<li>suffixes合并</li>
</ol>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> turtle <span class="keyword">import</span> right</span><br><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"></span><br><span class="line">left = pd.DataFrame(&#123;</span><br><span class="line">    <span class="string">&#x27;key&#x27;</span>:[<span class="string">&#x27;K0&#x27;</span>,<span class="string">&#x27;K1&#x27;</span>,<span class="string">&#x27;K2&#x27;</span>,<span class="string">&#x27;K3&#x27;</span>],</span><br><span class="line">    <span class="string">&#x27;A&#x27;</span>:[<span class="string">&#x27;A0&#x27;</span>,<span class="string">&#x27;A1&#x27;</span>,<span class="string">&#x27;A2&#x27;</span>,<span class="string">&#x27;A3&#x27;</span>],</span><br><span class="line">    <span class="string">&#x27;B&#x27;</span>:[<span class="string">&#x27;B0&#x27;</span>,<span class="string">&#x27;B1&#x27;</span>,<span class="string">&#x27;B2&#x27;</span>,<span class="string">&#x27;B3&#x27;</span>]</span><br><span class="line">&#125;)</span><br><span class="line">right = pd.DataFrame(&#123;</span><br><span class="line">    <span class="string">&#x27;key&#x27;</span>:[<span class="string">&#x27;K0&#x27;</span>,<span class="string">&#x27;K1&#x27;</span>,<span class="string">&#x27;K2&#x27;</span>,<span class="string">&#x27;K3&#x27;</span>],</span><br><span class="line">    <span class="string">&#x27;C&#x27;</span>:[<span class="string">&#x27;C0&#x27;</span>,<span class="string">&#x27;C1&#x27;</span>,<span class="string">&#x27;C2&#x27;</span>,<span class="string">&#x27;C3&#x27;</span>],</span><br><span class="line">    <span class="string">&#x27;D&#x27;</span>:[<span class="string">&#x27;D0&#x27;</span>,<span class="string">&#x27;D1&#x27;</span>,<span class="string">&#x27;D2&#x27;</span>,<span class="string">&#x27;C3&#x27;</span>]</span><br><span class="line">&#125;)</span><br><span class="line"><span class="built_in">print</span>(left)</span><br><span class="line"><span class="built_in">print</span>(right)</span><br><span class="line"><span class="comment"># 使用merge进行合并，on指定合并依据哪一列</span></span><br><span class="line">res = pd.merge(left,right,on = <span class="string">&#x27;key&#x27;</span>)</span><br><span class="line"><span class="built_in">print</span>(res)</span><br><span class="line"></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;************--1--***************&quot;</span>)</span><br><span class="line">left = pd.DataFrame(&#123;</span><br><span class="line">    <span class="string">&#x27;key1&#x27;</span>:[<span class="string">&#x27;K0&#x27;</span>,<span class="string">&#x27;K0&#x27;</span>,<span class="string">&#x27;K1&#x27;</span>,<span class="string">&#x27;K2&#x27;</span>],</span><br><span class="line">    <span class="string">&#x27;key2&#x27;</span>:[<span class="string">&#x27;K0&#x27;</span>,<span class="string">&#x27;K1&#x27;</span>,<span class="string">&#x27;K0&#x27;</span>,<span class="string">&#x27;K1&#x27;</span>],</span><br><span class="line">    <span class="string">&#x27;A&#x27;</span>:[<span class="string">&#x27;A0&#x27;</span>,<span class="string">&#x27;A1&#x27;</span>,<span class="string">&#x27;A2&#x27;</span>,<span class="string">&#x27;A3&#x27;</span>],</span><br><span class="line">    <span class="string">&#x27;B&#x27;</span>:[<span class="string">&#x27;B0&#x27;</span>,<span class="string">&#x27;B1&#x27;</span>,<span class="string">&#x27;B2&#x27;</span>,<span class="string">&#x27;B3&#x27;</span>]</span><br><span class="line">&#125;)</span><br><span class="line">right = pd.DataFrame(&#123;</span><br><span class="line">    <span class="string">&#x27;key1&#x27;</span>:[<span class="string">&#x27;K0&#x27;</span>,<span class="string">&#x27;K1&#x27;</span>,<span class="string">&#x27;K1&#x27;</span>,<span class="string">&#x27;K2&#x27;</span>],</span><br><span class="line">    <span class="string">&#x27;key2&#x27;</span>:[<span class="string">&#x27;K0&#x27;</span>,<span class="string">&#x27;K0&#x27;</span>,<span class="string">&#x27;K0&#x27;</span>,<span class="string">&#x27;K0&#x27;</span>],</span><br><span class="line">    <span class="string">&#x27;C&#x27;</span>:[<span class="string">&#x27;C0&#x27;</span>,<span class="string">&#x27;C1&#x27;</span>,<span class="string">&#x27;C2&#x27;</span>,<span class="string">&#x27;C3&#x27;</span>],</span><br><span class="line">    <span class="string">&#x27;D&#x27;</span>:[<span class="string">&#x27;D0&#x27;</span>,<span class="string">&#x27;D1&#x27;</span>,<span class="string">&#x27;D2&#x27;</span>,<span class="string">&#x27;C3&#x27;</span>]</span><br><span class="line">&#125;)</span><br><span class="line"><span class="comment"># print(left)</span></span><br><span class="line"><span class="comment"># print(right)</span></span><br><span class="line"><span class="comment"># 默认的合并方法，是inner.即只考虑相同的key，基于相同的key合并</span></span><br><span class="line">res = pd.merge(left,right,on=[<span class="string">&#x27;key1&#x27;</span>,<span class="string">&#x27;key2&#x27;</span>],how=<span class="string">&#x27;inner&#x27;</span>)</span><br><span class="line"><span class="built_in">print</span>(res)</span><br><span class="line"><span class="comment"># outer,对于不同的key，进行填充</span></span><br><span class="line">res = pd.merge(left,right,on=[<span class="string">&#x27;key1&#x27;</span>,<span class="string">&#x27;key2&#x27;</span>],how=<span class="string">&#x27;outer&#x27;</span>)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;**********--outer--**********&quot;</span>)</span><br><span class="line"><span class="built_in">print</span>(res)</span><br><span class="line"><span class="comment"># how = [&#x27;left&#x27;,&#x27;right&#x27;,&#x27;outer&#x27;,&#x27;inner&#x27;]</span></span><br><span class="line"><span class="comment"># left,right就是数据库的左外连接和右外连接，即基于左侧的key合并还是基于右侧的key合并</span></span><br><span class="line">res = pd.merge(left,right,on=[<span class="string">&#x27;key1&#x27;</span>,<span class="string">&#x27;key2&#x27;</span>],how=<span class="string">&#x27;left&#x27;</span>)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;*********--left--**********&quot;</span>)</span><br><span class="line"><span class="built_in">print</span>(res)</span><br><span class="line"></span><br><span class="line"><span class="comment"># indicator,显示数据是如何合并的</span></span><br><span class="line">df1 = pd.DataFrame(&#123;</span><br><span class="line">    <span class="string">&#x27;col1&#x27;</span>:[<span class="number">0</span>,<span class="number">1</span>],</span><br><span class="line">    <span class="string">&#x27;col_left&#x27;</span>:[<span class="string">&#x27;a&#x27;</span>,<span class="string">&#x27;b&#x27;</span>]</span><br><span class="line">&#125;)</span><br><span class="line">df2 = pd.DataFrame(&#123;</span><br><span class="line">    <span class="string">&#x27;col1&#x27;</span>:[<span class="number">1</span>,<span class="number">2</span>,<span class="number">2</span>],</span><br><span class="line">    <span class="string">&#x27;col_right&#x27;</span>:[<span class="number">2</span>,<span class="number">2</span>,<span class="number">2</span>]</span><br><span class="line">&#125;)</span><br><span class="line"><span class="comment"># 这里默认显示如何合并的列名为 _merge，用户可以自定义列名：indicator=&#x27;name&#x27;即可</span></span><br><span class="line">res = pd.merge(df1,df2,on=<span class="string">&#x27;col1&#x27;</span>,how=<span class="string">&#x27;outer&#x27;</span>,indicator=<span class="literal">True</span>)</span><br><span class="line"><span class="built_in">print</span>(res)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 通过index合并，需要使left_index和right_left都为真即可根据index合并</span></span><br><span class="line">left = pd.DataFrame(&#123;</span><br><span class="line">    <span class="string">&#x27;A&#x27;</span>:[<span class="string">&#x27;A0&#x27;</span>,<span class="string">&#x27;A1&#x27;</span>,<span class="string">&#x27;A2&#x27;</span>],</span><br><span class="line">    <span class="string">&#x27;B&#x27;</span>:[<span class="string">&#x27;B0&#x27;</span>,<span class="string">&#x27;B1&#x27;</span>,<span class="string">&#x27;B2&#x27;</span>]</span><br><span class="line">&#125;,index = [<span class="string">&#x27;K0&#x27;</span>,<span class="string">&#x27;K1&#x27;</span>,<span class="string">&#x27;K2&#x27;</span>])</span><br><span class="line">right = pd.DataFrame(&#123;</span><br><span class="line">    <span class="string">&#x27;C&#x27;</span>:[<span class="string">&#x27;C0&#x27;</span>,<span class="string">&#x27;C1&#x27;</span>,<span class="string">&#x27;C2&#x27;</span>],</span><br><span class="line">    <span class="string">&#x27;D&#x27;</span>:[<span class="string">&#x27;D0&#x27;</span>,<span class="string">&#x27;D1&#x27;</span>,<span class="string">&#x27;D2&#x27;</span>]</span><br><span class="line">&#125;,index = [<span class="string">&#x27;K0&#x27;</span>,<span class="string">&#x27;K2&#x27;</span>,<span class="string">&#x27;K3&#x27;</span>])</span><br><span class="line">res = pd.merge(left,right,left_index=<span class="literal">True</span>,right_index=<span class="literal">True</span>,how=<span class="string">&#x27;outer&#x27;</span>)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;*********--index--**********&quot;</span>)</span><br><span class="line"><span class="built_in">print</span>(res)</span><br><span class="line"></span><br><span class="line"><span class="comment"># suffixes对原列相同，但是表示的意义不同的列，进行重命名</span></span><br><span class="line"><span class="comment"># 比如这个，都是年龄，但是分为男孩年龄和女孩年龄，</span></span><br><span class="line"><span class="comment"># 如果直接根据关键字合并，那么只剩下age这一列，无法区分性别，所以使用suffixes</span></span><br><span class="line">boys = pd.DataFrame(&#123;</span><br><span class="line">    <span class="string">&#x27;k&#x27;</span>:[<span class="string">&#x27;K0&#x27;</span>,<span class="string">&#x27;K1&#x27;</span>,<span class="string">&#x27;K2&#x27;</span>],</span><br><span class="line">    <span class="string">&#x27;age&#x27;</span>:[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]</span><br><span class="line">&#125;)</span><br><span class="line">girls = pd.DataFrame(&#123;</span><br><span class="line">    <span class="string">&#x27;k&#x27;</span>:[<span class="string">&#x27;K0&#x27;</span>,<span class="string">&#x27;K0&#x27;</span>,<span class="string">&#x27;K3&#x27;</span>],</span><br><span class="line">    <span class="string">&#x27;age&#x27;</span>:[<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>]</span><br><span class="line">&#125;)</span><br><span class="line"><span class="built_in">print</span>(boys)</span><br><span class="line"><span class="built_in">print</span>(girls)</span><br><span class="line"><span class="comment"># 这里对于K0，男孩年龄将会用age_boy表示，女孩年龄会用age_girl表示</span></span><br><span class="line">res = pd.merge(boys,girls,on = <span class="string">&#x27;k&#x27;</span>,how = <span class="string">&#x27;outer&#x27;</span>,suffixes=[<span class="string">&#x27;_boy&#x27;</span>,<span class="string">&#x27;_girl&#x27;</span>])</span><br><span class="line"><span class="built_in">print</span>(res)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="8、plot画图">8、plot画图</h2>
<p>主要是用到matplotlib模块，这里不再详细介绍</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"><span class="keyword">import</span> matplotlib.pyplot <span class="keyword">as</span> plt</span><br><span class="line"><span class="comment"># plot data</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Series</span></span><br><span class="line">data = pd.Series(np.random.randn(<span class="number">1000</span>),index=np.arange(<span class="number">1000</span>))</span><br><span class="line"><span class="comment"># 累加</span></span><br><span class="line">data = data.cumsum()</span><br><span class="line"><span class="comment"># 在pandas中直接将数据plot就可</span></span><br><span class="line">data.plot()</span><br><span class="line">plt.show()</span><br><span class="line"></span><br><span class="line"><span class="comment"># DataFrame</span></span><br><span class="line">data = pd.DataFrame(np.random.randn(<span class="number">1000</span>,<span class="number">4</span>),<span class="comment">#生成一千组数据，一组四个数据</span></span><br><span class="line">                    index = np.arange(<span class="number">1000</span>),</span><br><span class="line">                    columns=<span class="built_in">list</span>(<span class="string">&#x27;ABCD&#x27;</span>))</span><br><span class="line">data = data.cumsum()</span><br><span class="line"><span class="comment"># 先查看一下前5个数据，默认为5，可以在括号中传入参数改变数据</span></span><br><span class="line"><span class="built_in">print</span>(data.head())</span><br><span class="line">data.plot()</span><br><span class="line">plt.show()</span><br></pre></td></tr></table></figure>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://zhaoyunlai.gitee.io">zylai</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://zhaoyunlai.gitee.io/posts/8dfe3ff98cbb/">https://zhaoyunlai.gitee.io/posts/8dfe3ff98cbb/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://zhaoyunlai.gitee.io" target="_blank">zylai</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/python%E6%95%B0%E6%8D%AE%E5%88%86%E6%9E%90/">python数据分析</a></div><div class="post_share"><div class="social-share" data-image="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fwww.geocalculate.com%2Fwp-content%2Fuploads%2F2020%2F07%2Fpandas.png&amp;refer=http%3A%2F%2Fwww.geocalculate.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=auto?sec=1653547694&amp;t=2523b5ca63fc839243af3a93c7c2ba71" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/posts/68452ae5cbff/"><img class="prev-cover" src="https://zylai-cloud-pic-1306915061.cos.ap-nanjing.myqcloud.com/images01/image-20220424130422953.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">【cs231n】图像分类 K最邻近算法 线性分类器</div></div></a></div><div class="next-post pull-right"><a href="/posts/ee38083b3187/"><img class="next-cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fmmbiz.qpic.cn%2Fmmbiz_gif%2Fptp8P184xjyNYtPWdCkYyJrGRibqWpKXflz2vKmsNVmLV4sYmAHN0CUQ7AqrkShBE4fOyeHAhSJicaX7UgxHXlPw%2F640%3Fwx_fmt%3Dgif&amp;refer=http%3A%2F%2Fmmbiz.qpic.cn&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=auto?sec=1653547607&amp;t=8c6a499edf55691baed0949ed7465a71" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">numpy</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/posts/ee38083b3187/" title="numpy"><img class="cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fmmbiz.qpic.cn%2Fmmbiz_gif%2Fptp8P184xjyNYtPWdCkYyJrGRibqWpKXflz2vKmsNVmLV4sYmAHN0CUQ7AqrkShBE4fOyeHAhSJicaX7UgxHXlPw%2F640%3Fwx_fmt%3Dgif&refer=http%3A%2F%2Fmmbiz.qpic.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1653547607&t=8c6a499edf55691baed0949ed7465a71" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-03-07</div><div class="title">numpy</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/img/avatar.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">zylai</div><div class="author-info__description">zylai && 管大虾の克星</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">18</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">14</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">11</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/zhaoyunlai"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/zhaoyunlai" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="mailto:zylai0712@163.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">This is my blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link"><span class="toc-text">Pandas</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1%E3%80%81%E5%9F%BA%E6%9C%AC%E4%BB%8B%E7%BB%8D"><span class="toc-text">1、基本介绍</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2%E3%80%81%E9%80%89%E6%8B%A9%E6%95%B0%E6%8D%AE"><span class="toc-text">2、选择数据</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3%E3%80%81%E8%AE%BE%E7%BD%AE%E5%80%BC"><span class="toc-text">3、设置值</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4%E3%80%81%E5%A4%84%E7%90%86%E4%B8%A2%E5%A4%B1%E6%95%B0%E6%8D%AE"><span class="toc-text">4、处理丢失数据</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5%E3%80%81%E5%AF%BC%E5%85%A5%E5%92%8C%E5%AF%BC%E5%87%BA"><span class="toc-text">5、导入和导出</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#6%E3%80%81%E5%90%88%E5%B9%B6concat"><span class="toc-text">6、合并concat</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#7%E3%80%81%E5%90%88%E5%B9%B6merge"><span class="toc-text">7、合并merge</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#8%E3%80%81plot%E7%94%BB%E5%9B%BE"><span class="toc-text">8、plot画图</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/posts/22ef9dff46af/" title="移动互联网开发技术-第三次实验"><img src="https://zylai-cloud-pic-1306915061.cos.ap-nanjing.myqcloud.com/images01/%E7%B3%BB%E7%BB%9F%E8%AE%BE%E8%AE%A1.assets/sdnu-background2.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="移动互联网开发技术-第三次实验"/></a><div class="content"><a class="title" href="/posts/22ef9dff46af/" title="移动互联网开发技术-第三次实验">移动互联网开发技术-第三次实验</a><time datetime="2022-10-15T01:43:30.000Z" title="发表于 2022-10-15 09:43:30">2022-10-15</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/85838fb07a8b/" title="学生成长动态追踪系统-学生使用手册"><img src="https://zylai-cloud-pic-1306915061.cos.ap-nanjing.myqcloud.com/images01/%E7%B3%BB%E7%BB%9F%E8%AE%BE%E8%AE%A1.assets/sdnu-background2.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="学生成长动态追踪系统-学生使用手册"/></a><div class="content"><a class="title" href="/posts/85838fb07a8b/" title="学生成长动态追踪系统-学生使用手册">学生成长动态追踪系统-学生使用手册</a><time datetime="2022-09-25T11:43:30.000Z" title="发表于 2022-09-25 19:43:30">2022-09-25</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/896511b44bb7/" title="学生成长动态追踪系统-家长使用手册"><img src="https://zylai-cloud-pic-1306915061.cos.ap-nanjing.myqcloud.com/images01/%E7%B3%BB%E7%BB%9F%E8%AE%BE%E8%AE%A1.assets/sdnu-background2.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="学生成长动态追踪系统-家长使用手册"/></a><div class="content"><a class="title" href="/posts/896511b44bb7/" title="学生成长动态追踪系统-家长使用手册">学生成长动态追踪系统-家长使用手册</a><time datetime="2022-09-25T11:43:29.000Z" title="发表于 2022-09-25 19:43:29">2022-09-25</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/78afc3f0ab42/" title="Shiro权限安全框架的使用"><img src="https://zylai-cloud-pic-1306915061.cos.ap-nanjing.myqcloud.com/images01/shiro.assets/image-20220929162947122.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Shiro权限安全框架的使用"/></a><div class="content"><a class="title" href="/posts/78afc3f0ab42/" title="Shiro权限安全框架的使用">Shiro权限安全框架的使用</a><time datetime="2022-09-18T11:43:29.000Z" title="发表于 2022-09-18 19:43:29">2022-09-18</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/e32230f09e09/" title="学生成长动态追踪系统"><img src="https://zylai-cloud-pic-1306915061.cos.ap-nanjing.myqcloud.com/images01/%E7%B3%BB%E7%BB%9F%E8%AE%BE%E8%AE%A1.assets/sdnu-background2.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="学生成长动态追踪系统"/></a><div class="content"><a class="title" href="/posts/e32230f09e09/" title="学生成长动态追踪系统">学生成长动态追踪系统</a><time datetime="2022-08-25T11:43:29.000Z" title="发表于 2022-08-25 19:43:29">2022-08-25</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fwww.geocalculate.com%2Fwp-content%2Fuploads%2F2020%2F07%2Fpandas.png&amp;refer=http%3A%2F%2Fwww.geocalculate.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=auto?sec=1653547694&amp;t=2523b5ca63fc839243af3a93c7c2ba71')"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2022 By zylai</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><script src="/js/search/local-search.js"></script><div class="js-pjax"></div><script defer="defer" id="ribbon" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/canvas-ribbon.min.js" size="150" alpha="0.6" zIndex="-1" mobile="false" data-click="true"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><!-- hexo injector body_end start --><script data-pjax>function electric_clock_injector_config(){
                var parent_div_git = document.getElementsByClassName('sticky_layout')[0];
                var item_html = '<div class="card-widget card-clock"><div class="card-glass"><div class="card-background"><div class="card-content"><div id="hexo_electric_clock"><img id="card-clock-loading" src="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-clock/clock/images/weather/loading.gif" style="height: 120px; width: 100%;" data-ll-status="loading" class="entered loading"></div></div></div></div></div>';
                console.log('已挂载electric_clock')
                // parent_div_git.innerHTML=item_html+parent_div_git.innerHTML // 无报错，但不影响使用(支持pjax跳转)
                parent_div_git.insertAdjacentHTML("afterbegin",item_html) // 有报错，但不影响使用(支持pjax跳转)
            }if( document.getElementsByClassName('sticky_layout')[0] && (location.pathname ==='/'|| '/' ==='all')){

            electric_clock_injector_config()
        } </script><script src="https://pv.sohu.com/cityjson?ie=utf-8"></script><script data-pjax  src="https://cdn.jsdelivr.net/gh/Zfour/hexo-electric-clock@1.0.6/clock.js"></script><!-- hexo injector body_end end --></body></html>